package com.anruisi.hxmes.auth.util;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author cmy
 * @date 2021/1/8 10:15
 */
@Slf4j
public class RespResult {


    /**
     * 成功
     * @param response
     * @param msg
     */
    public static void success(HttpServletResponse response, String msg){
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            out = response.getWriter();
            ResultBean resultBean = new ResultBean(200,true,msg);
            out.println(JSON.toJSONString(resultBean));
        } catch (Exception e) {
            log.error("RespResult____JSON输出异常"+e);
        }finally{
            if(out!=null){
                out.flush();
                out.close();
            }
        }
    }

    /**
     * 成功
     * @param response
     * @param msg
     * @param data 指定返回数据
     */
    public static void success(HttpServletResponse response, String msg, Object data){
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            out = response.getWriter();
            ResultBean resultBean = new ResultBean(200,data,msg);
            out.println(JSON.toJSONString(resultBean));
        } catch (Exception e) {
            log.error("RespResult____JSON输出异常"+e);
        }finally{
            if(out!=null){
                out.flush();
                out.close();
            }
        }
    }


    /**
     * 失败返回
     * @param response
     * @param msg
     */
    public static void failure(HttpServletResponse response, String msg){
        PrintWriter out = null;
        try {
            response.setStatus(400);
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            out = response.getWriter();
            ResultBean resultBean = new ResultBean(400,null,msg);
            out.println(JSON.toJSONString(resultBean));
        } catch (Exception e) {
            log.error("RespResult____JSON输出异常"+e);
        }finally{
            if(out!=null){
                out.flush();
                out.close();
            }
        }
    }

    /**
     * 无权限
     * @param response
     * @param msg
     */
    public static void accessDenied(HttpServletResponse response, String msg){
        PrintWriter out = null;
        try {
            response.setStatus(401);
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            out = response.getWriter();
            ResultBean resultBean = new ResultBean(401,null,msg);
            out.println(JSON.toJSONString(resultBean));
        } catch (Exception e) {
            log.error("RespResult____JSON输出异常"+e);
        }finally{
            if(out!=null){
                out.flush();
                out.close();
            }
        }
    }

}
